﻿using M_Process.API.Appliction.Command.ReportingCommand;
using M_Process.Domain.DTO;
using M_Process.Repository.ProcessInterface;
using MediatR;

namespace M_Process.API.Appliction.CommandHandlers.ReportingCommandHandler
{
    public class ShowReportingListCommandHandler : IRequestHandler<ShowReportingListCommand, ReportingDto<ReportingProcessTaskDto>>
    {
        /// <summary>
        /// 报工仓储
        /// </summary>
        protected readonly IReportingRepository reportingRepository;

        /// <summary>
        /// 工序任务仓储接口
        /// </summary>
        protected readonly IProcessTasksRepository processTasksRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="reportingRepository">报工仓储</param>
        /// <param name="processTasksRepository">工序任务仓储接口</param>
        public ShowReportingListCommandHandler(IReportingRepository reportingRepository, IProcessTasksRepository processTasksRepository)
        {
            this.reportingRepository = reportingRepository;
            this.processTasksRepository = processTasksRepository;
        }

        public async Task<ReportingDto<ReportingProcessTaskDto>> Handle(ShowReportingListCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await reportingRepository.GetAllAsync()
                       join b in await processTasksRepository.GetAllAsync()
                       on a.ProcessTasksId equals b.ProcessTasksId
                       where a.IsDeleted == false && b.IsDeleted == false
                       select new ReportingProcessTaskDto
                       {
                           ReportingId = a.ReportingId,
                           ReportingNum = a.ReportingNum,
                           ReportingNumber = a.ReportingNumber,
                           EquipmentName = a.EquipmentName,
                           EquipmentNumber = a.EquipmentNumber,
                           Station = a.Station,
                           TeamsAndGroups = a.TeamsAndGroups,
                           ProcessTasksId = b.ProcessTasksId,
                           TaskNumber = b.TaskNumber,
                           ProductionBatch = b.ProductionBatch,
                           ProcessName = b.ProcessName,
                           ProductNumber = b.ProductNumber,
                           Operator=a.Operator,
                           CreatedBy = b.CreatedBy,
                           CreatedDate = b.CreatedDate,
                           IsDeleted = b.IsDeleted
                       };

            if (!string.IsNullOrEmpty(request.ReportingNumber))
            {
                list = list.Where(x => x.ReportingNumber.Contains(request.ReportingNumber));
            }

            if(!string.IsNullOrEmpty(request.TaskNumber))
            {
                list = list.Where(x => x.TaskNumber.Contains(request.TaskNumber));
            }

            if (request.PageIndex == null)
            {
                request.PageIndex = 1;
            }

            if (request.PageSize == null)
            {
                request.PageSize = 15;
            }

            ReportingDto<ReportingProcessTaskDto> page = new ReportingDto<ReportingProcessTaskDto>();
            page.TotalCount = list.Count();
            page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / (int)request.PageSize);
            page.list = list.OrderByDescending(x => x.CreatedDate).Skip((int)request.PageIndex - 1).Take((int)request.PageSize).ToList();
            return page;
        }
    }
}
